package com.butterfly.vv;

import com.beem.project.beem.BeemApplication;
import com.beem.project.beem.R;
import com.beem.project.beem.ui.LoginAnim;
import com.butterfly.vv.vv.utils.Debug;
import com.teleca.jamendo.api.Account;
import com.teleca.jamendo.dialog.PhoneNoLoadingDialog;
import com.teleca.jamendo.dialog.RegistryLoadingDialog;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class VVaccountSwitch extends Activity {
	
	Account mAccount;
	
	private SharedPreferences mSettings;
	
	public static void phoneNolaunch(Activity c, String []query){
		new PhoneNoLoadingDialog(c,R.string.artist_loading, R.string.artist_fail).execute(query);
	}
	
	public static void phoneNolaunch(Activity c, String []query,String jid){
		new PhoneNoLoadingDialog(c,R.string.artist_loading, R.string.artist_fail,jid).execute(query);
	}
	
	private static final int LOGIN_REQUEST_CODE = 1;
	Button nest;
	EditText username;
	EditText password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mAccount = (Account) getIntent().getSerializableExtra("mAccount");
		//Log.i("tag", mAccount.getAccount());
		setContentView(R.layout.vv_wizard_account_configure);
		
		mSettings = PreferenceManager.getDefaultSharedPreferences(this);
		String accountName = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
		username = (EditText) findViewById(R.id.account_username);
		password = (EditText) findViewById(R.id.account_password);
		username.setText(accountName);
		password.setText(mSettings.getString(BeemApplication.ACCOUNT_PASSWORD_KEY, ""));
		//SharedPreferences.Editor edit = mSettings.edit();
		
		nest = (Button) findViewById(R.id.next);
		nest.setEnabled(true);
		nest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveCredential(username.getText().toString(), password.getText().toString(), mSettings);
				Intent i = new Intent(getApplicationContext(), LoginAnim.class);
			    startActivityForResult(i, LOGIN_REQUEST_CODE);
			}
		});
		Button button = (Button) findViewById(R.id.manual_setup);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), com.beem.project.beem.ui.wizard.Account.class);
				startActivity(i);
			}
		});
		
	}
	
	private void saveCredential(String jid, String pass,SharedPreferences settings) {
		Debug.getDebugInstance().log("---"+jid+"--"+pass);
		SharedPreferences.Editor edit = settings.edit();
		edit.putString(BeemApplication.ACCOUNT_USERNAME_KEY, jid);
		edit.putString(BeemApplication.ACCOUNT_PASSWORD_KEY, pass);
		edit.putBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false);
		edit.commit();
	    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == LOGIN_REQUEST_CODE) {
			Debug.getDebugInstance().log("ggg "+getClass().getName()+" "+resultCode);
			if (resultCode == Activity.RESULT_OK) {
				//startActivity(new Intent(getApplicationContext(), cls));
				setResult(Activity.RESULT_OK);
				finish();
			} else if (resultCode == Activity.RESULT_CANCELED) {
				setResult(Activity.RESULT_CANCELED);
			}
		}
	}
}
